﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Linq;
using Atrax.Contracts;
using Atrax.Library;
using Atrax.Logging;

namespace Atrax.Master
{
	internal static class PersistenceManager
	{
		static string m_Dir = null;

		internal static List<Query> LoadQuerys()
		{
			Initialize();
			List<Query> list = new List<Query>();
			try
			{
				string[] files = Directory.GetFiles(m_Dir, "*.dat");
				foreach (string file in files)
				{
					Query query = LoadQueryFromFile(file);
					if (query != null) list.Add(query);
				}
			}
			catch (Exception e)
			{
				Log.App(LogEvents.PersistenceManagerLoadQuerysFailed, e);
			}
			return list;
		}

		internal static void Remove(string queryId)
		{
			Initialize();
			string file = GetFileName(queryId);
			if (File.Exists(file))
			{
				try
				{
					File.Delete(file);
				}
				catch (Exception e)
				{
					Log.App(LogEvents.PersistenceManagerRemoveFailed, "queryid=" + queryId, e);
				}
			}
		}

		internal static Query LoadQuery(string queryId)
		{
			Initialize();
			string file = GetFileName(queryId);
			return LoadQueryFromFile(file);
		}

		static Query LoadQueryFromFile(string file)
		{
			if (File.Exists(file))
			{
				using (FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
				{
					Query query = null;
					try
					{
						BinaryFormatter formatter = new BinaryFormatter();
						query = (Query)formatter.Deserialize(fs);
					}
					catch (Exception e)
					{
						Log.App(LogEvents.PersistenceManagerLoadQueryFromFileFailed, "file=" + file, e);
					}
					return query;
				}
			}
			else
				return null;
		}

		internal static void SaveQuery(Query query)
		{
			Initialize();
			string file = GetFileName(query.Header.QueryId);
			try
			{
				using (FileStream fs = File.Open(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
				{
					BinaryFormatter formatter = new BinaryFormatter();
					formatter.Serialize(fs, query);
				}
			}
			catch (Exception e)
			{
				Log.App(LogEvents.PersistenceManagerSaveQueryFailed, e);
			}
		}

		static string GetFileName(string queryId)
		{
			return m_Dir + "\\" + queryId + ".dat";
		}

		static void Initialize()
		{
			if (m_Dir == null) m_Dir = AppDomain.CurrentDomain.GetData("DataDirectory") as string; //a little hack to get the path to the App_Data folder
		}
	}
}
